package com.mls.fivedistrict.controller.portal;

import com.mls.fivedistrict.common.PageFilter;
import com.mls.fivedistrict.common.ServiceResponse;
import com.mls.fivedistrict.controller.BaseController;
import com.mls.fivedistrict.enums.TaskTypeEnum;
import com.mls.fivedistrict.pojo.Task;
import com.mls.fivedistrict.pojo.UserTask;
import com.mls.fivedistrict.server.TaskService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Created by chenxiuxiang on 2018/3/28.
 */
@RestController("portalTaskController")
@RequestMapping("/api/app")
public class TaskController extends BaseController {
    @Resource(name = "taskService")
    TaskService taskService;

    @GetMapping(path = "/v1/tasks")
    public ServiceResponse getTaks(HttpServletRequest httpServletRequest) {
        PageFilter pageFilter = getPageFilter();
        pageFilter.getFilterMap().put("userId", getUser().getId() + "");
        return taskService.getUserTask(pageFilter);
    }

    @GetMapping(path = "/v1/task/sys")
    public ServiceResponse getSysTaks() {
        PageFilter pageFilter = getPageFilter();
        pageFilter.getFilterMap().put("type", TaskTypeEnum.SYSTEMTASk.getValue());
        pageFilter.getFilterMap().put("userId", getUser().getId() + "");
        return taskService.getUserTask(pageFilter);
    }

    @GetMapping(path = "/v1/task/fixed")
    public ServiceResponse getFixedTaks() {
        PageFilter pageFilter = getPageFilter();
        pageFilter.getFilterMap().put("type", TaskTypeEnum.FIXEDTASK.getValue());
        pageFilter.getFilterMap().put("userId", getUser().getId() + "");
        return taskService.getUserTask(pageFilter);
    }

    @PostMapping(path = "/v1/task/complete/{taskId}")
    public ServiceResponse completeTask(@PathVariable Long taskId) {
        Task task = taskService.gettaskDetail(taskId);
        if (task == null) {
            return ServiceResponse.createErrorResponse("任务不存在");
        }
        int i = taskService.completeTask(taskId, getUser().getId(), task.getName());
        if (i > 0) {
            return ServiceResponse.createSuccessResponse();
        } else {
            return ServiceResponse.createErrorResponse("操作失败");
        }

    }


    @PostMapping(path = "/v1/task/complete/bind/{taskId}")
    public ServiceResponse completeBindTask(@PathVariable Long taskId, @RequestBody Map params) {

        if (params.get("G_USER_ID") == null) {
            return ServiceResponse.createErrorResponse("国盾用户不存在");
        }

        int i = taskService.completeBindTask(taskId, getUser().getId(), params);
        if (i > 0) {
            return ServiceResponse.createSuccessResponse();
        } else {
            return ServiceResponse.createErrorResponse("操作失败");
        }
    }

    /**
     * 用户获取是否已经人脸识别
     *
     * @return
     */
    @GetMapping(path = "/v1/task/face")
    public ServiceResponse isFace(HttpServletRequest httpServletRequest) {
        UserTask userIsFace = taskService.getUserIsFace(getUser().getId());
        if (userIsFace == null) {
            return ServiceResponse.createSuccessResponse("false");
        } else {
            return ServiceResponse.createSuccessResponse("true");
        }
    }
}
